package com.young.weixin.model.message;


import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.young.common.util.XmlUtil;
import com.young.weixin.model.enums.EWxMessageTypeEnum;

/**
 * 微信消息基类
 *
 * @author wangjiyu
 * @date 2021/10/5 17:03
 */
public abstract class WxBaseMessage implements IWxMessageFactoryBean<WxBaseMessage> {
    /**
     * 收消息人的openid
     */
    @XStreamAlias("ToUserName")
    private String toUserName;
    /**
     * 发消息人的openid
     */
    @XStreamAlias("FromUserName")
    private String fromUserName;
    /**
     * 消息时间 单位秒
     */
    @XStreamAlias("CreateTime")
    private String createTime;
    /**
     * 消息类型
     */
    @XStreamAlias("MsgType")
    private String msgType;
    /**
     * 消息id
     */
    @XStreamAlias("MsgId")
    private String msgId;

    public WxBaseMessage() {
    }

    /**
     * 判断是否事件消息
     * @return true表示是事件消息
     */
    public boolean isEvent() {
        return this.msgTypeIs(EWxMessageTypeEnum.EVENT);
    }

    /**
     * 判断消息类型
     *
     * @param typeEnum
     * @return true表示匹配成功
     */
    public boolean msgTypeIs(EWxMessageTypeEnum typeEnum) {
        return typeEnum.eq(this.msgType);
    }

    /**
     * 消息类型枚举
     *
     * @return
     */
    public EWxMessageTypeEnum msgType() {
        return EWxMessageTypeEnum.of(this.msgType);
    }

    /**
     * 转为xml
     *
     * @return
     */
    public String toXml() {
        return XmlUtil.beanToXml(this);
    }

    /**
     * 填充消息公参
     *
     * @param mapWrapper 消息map保证对象
     */
    public void fillWxBaseMessage(WxMessageMapWrapper mapWrapper) {
        this.setToUserName(mapWrapper.getToUserName());
        this.setFromUserName(mapWrapper.getFromUserName());
        this.setCreateTime(mapWrapper.getCreateTime());
        this.msgType = mapWrapper.getMsgType();
        this.setMsgId(mapWrapper.getMsgId());
    }

    /**
     * 填充消息对象
     *
     * @param fromUser        发送人
     * @param toUser          接收人
     * @param messageTypeEnum 消息类型
     * @return 消息对象
     */
    public void fillWxBaseMessage(String fromUser, String toUser, EWxMessageTypeEnum messageTypeEnum) {
        this.setFromUserName(fromUser);
        this.setToUserName(toUser);
        this.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
        this.setMsgType(messageTypeEnum.type);
    }

    /**
     * 填充回复消息的用户属性
     *
     * @param userMessage 微信调用传入的用户消息对象
     */
    public void fillBackMessage(WxBaseMessage userMessage) {
        this.setFromUserName(userMessage.getToUserName());
        this.setToUserName(userMessage.getFromUserName());
        this.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
    }

    public String getToUserName() {
        return toUserName;
    }

    public void setToUserName(String toUserName) {
        this.toUserName = toUserName;
    }

    public String getFromUserName() {
        return fromUserName;
    }

    public void setFromUserName(String fromUserName) {
        this.fromUserName = fromUserName;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getMsgType() {
        return msgType;
    }

    public void setMsgType(String msgType) {
        this.msgType = msgType;
    }

    public String getMsgId() {
        return msgId;
    }

    public void setMsgId(String msgId) {
        this.msgId = msgId;
    }
}
